﻿Imports System.Windows.Forms
Imports System.Net
Imports System.Data.SqlClient
Imports System.IO

Public Class OrderDialog
    Public Serial As String
    Public dKey As String
    Public Guestname As String
    Public GuestAddress As String
    Public ContactNo As String
    Public order_ As String
    Public totalBill As String
    Public specialIns As String
    Public ChangeFor As String
    Public Status As String
    Public DateSent As String
    Public Sender_ As String
    Public Credential As Integer
    Public DateModified As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'Tag as Close
        If txtstatus.Text <> "CLOSE" And mainform.AccountType = 2 Then
            If MsgBox("Click Yes to Confirm", vbYesNo) = vbYes Then
                OK_Button.Text = "Please Wait..."
                Try
                    For Each cont As Control In Me.Controls
                        cont.Enabled = False
                    Next
                Catch ex As Exception
                End Try
                Dim t As New Threading.Thread(AddressOf ActionThread)
                t.Start()
            End If
        Else
            Me.Close()
        End If
        Try
            File.Delete(My.Application.Info.DirectoryPath & "\OrderNote" & Serial & ".txt")
        Catch ex As Exception

        End Try
    End Sub
    Sub ActionThread()
        If mainform.AccountType = 2 And Status <> "CLOSE" Then
            If WebCloseOrder() = True Then
                DialogResult = Windows.Forms.DialogResult.OK
            Else
                Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            End If
        ElseIf mainform.AccountType = 1 And Status <> "CLOSE" Then
            If SQLCloseOrder() = True Then
                DialogResult = Windows.Forms.DialogResult.OK
            Else
                Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            End If
        End If
        Me.Close()
    End Sub
    Function SQLCloseOrder() As Boolean
        Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
        Me.Width = 350
        Try
            Dim myConn As SqlConnection
            Dim myCmd As SqlCommand
            myConn = New SqlConnection(mainform.ConnectionString)
            myCmd = myConn.CreateCommand
            myCmd.CommandText = "UPDATE " & mainform.DBTable & " SET status = 'CLOSE' where dKey=" & dKey

            myConn.Open()
            myCmd.ExecuteNonQuery()

            Return True
            ''Me.Width = 350
        Catch ex As Exception
            Return False
        End Try
    End Function
    Function WebCloseOrder() As Boolean
        Try
            Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
            Me.Width = 350
            Dim myQuery As String = "mark=CLOSE&dKey=" & dKey
            Dim Webreq As HttpWebRequest = DirectCast(WebRequest.Create(mainform.Host & ":" & mainform.port & "/" & mainform.MarkPage & "?" & myQuery), HttpWebRequest)
            Webreq.Method = "GET"
            Webreq.KeepAlive = False

            Dim postresponse As HttpWebResponse
            postresponse = DirectCast(Webreq.GetResponse(), HttpWebResponse)

            If postresponse.StatusCode = HttpStatusCode.OK Then
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try
        Return False
    End Function

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
        Try
            File.Delete(My.Application.Info.DirectoryPath & "\OrderNote" & Serial & ".txt")
        Catch ex As Exception

        End Try
    End Sub

    Private Sub OrderDialog_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Try
            File.Delete(My.Application.Info.DirectoryPath & "\OrderNote" & Serial & ".txt")
        Catch ex As Exception

        End Try
    End Sub

    Private Sub OrderDialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "ORDER - " & Serial
        txtguestname.Text = Guestname
        txtaddress.Text = GuestAddress
        txtcontactno.Text = ContactNo
        txtorder.Text = order_
        txttotalbill.Text = totalBill
        txtspecialins.Text = specialIns
        txtchangefor.Text = ChangeFor
        txtmodified.Text = DateModified
        txtstatus.Text = Status

        'Delete ExportNotepad
        Try
            File.Delete(My.Application.Info.DirectoryPath & "\OrderNote" & Serial & ".txt")
        Catch ex As Exception

        End Try
    End Sub

    Private Sub NotepadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NotepadToolStripMenuItem.Click
        Try
            Dim FILE_NAME As String = My.Application.Info.DirectoryPath & "\OrderNote" & Serial & ".txt"
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
            objWriter.WriteLine("Order :" & Serial)
            objWriter.WriteLine("Guest :" & Guestname)
            objWriter.WriteLine("Address :" & GuestAddress)
            objWriter.WriteLine("Contact :" & ContactNo)
            objWriter.WriteLine("Order :" & order_)
            objWriter.WriteLine("Total Bill :" & totalBill)
            objWriter.WriteLine("Special Instruction :" & specialIns)
            objWriter.WriteLine("Change For :" & ChangeFor)

            objWriter.Close()
            System.Diagnostics.Process.Start("notepad.exe", My.Application.Info.DirectoryPath & "\OrderNote" & Serial & ".txt")
        Catch ex As Exception
        End Try
    End Sub
End Class
